package cn.gcyl.module.biz.service.membershipsettings;

import cn.gcyl.framework.common.pojo.PageResult;
import cn.gcyl.framework.common.util.object.BeanUtils;
import cn.gcyl.module.biz.controller.admin.membershipsettings.vo.*;
import cn.gcyl.module.biz.dal.dataobject.membershipsettings.MembershipSettingsDO;
import cn.gcyl.module.biz.dal.mysql.membershipsettings.MembershipSettingsMapper;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.gcyl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.gcyl.module.system.enums.ErrorCodeConstants.MEMBERSHIP_SETTINGS_NOT_EXISTS;

/**
 * 会员设置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MembershipSettingsServiceImpl implements MembershipSettingsService {

    @Resource
    private MembershipSettingsMapper membershipSettingsMapper;

    @Override
    public Long createMembershipSettings(MembershipSettingsSaveReqVO createReqVO) {
        // 插入
        MembershipSettingsDO membershipSettings = BeanUtils.toBean(createReqVO, MembershipSettingsDO.class);
        membershipSettingsMapper.insert(membershipSettings);

        // 返回
        return membershipSettings.getId();
    }

    @Override
    public void updateMembershipSettings(MembershipSettingsSaveReqVO updateReqVO) {
        // 校验存在
        validateMembershipSettingsExists(updateReqVO.getId());
        // 更新
        MembershipSettingsDO updateObj = BeanUtils.toBean(updateReqVO, MembershipSettingsDO.class);
        membershipSettingsMapper.updateById(updateObj);
    }

    @Override
    public void deleteMembershipSettings(Long id) {
        // 校验存在
        validateMembershipSettingsExists(id);
        // 删除
        membershipSettingsMapper.deleteById(id);
    }
    @Override
    public List<MembershipSettingsDO> getMembershipSettingsList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return membershipSettingsMapper.selectByIds(ids);
    }


    @Override
        public void deleteMembershipSettingsListByIds(List<Long> ids) {
        // 删除
        membershipSettingsMapper.deleteByIds(ids);
        }


    private void validateMembershipSettingsExists(Long id) {
        if (membershipSettingsMapper.selectById(id) == null) {
            throw exception(MEMBERSHIP_SETTINGS_NOT_EXISTS);
        }
    }

    @Override
    public MembershipSettingsDO getMembershipSettings(Long id) {
        return membershipSettingsMapper.selectById(id);
    }

    @Override
    public PageResult<MembershipSettingsDO> getMembershipSettingsPage(MembershipSettingsPageReqVO pageReqVO) {
        return membershipSettingsMapper.selectPage(pageReqVO);
    }
    @Override
    public List<MembershipSettingsDO> getMembershipSettingsList(MembershipSettingsPageReqVO pageReqVO) {
        return membershipSettingsMapper.getMembershipSettingsList(pageReqVO);
    }

}